//
//  HearderView.m
//  YanMusicPlayer
//
//  Created by 1564335 on 15-6-11.
//  Copyright (c) 2015年 胡岩 All rights reserved.
//

#import "HearderView.h"

@interface HearderView ()<UITableViewDataSource, UITableViewDelegate>
{
    CGFloat y;
}
@property(nonatomic, assign) CGPoint startPoint;

@property(nonatomic, assign) CGFloat totalDistance;

@property(nonatomic, assign) CGFloat totalX;
@property(nonatomic, assign) CGFloat totalY;

@property(nonatomic, assign) CGFloat startY;

@property(nonatomic, assign) CGFloat scaleBtnX;
@property(nonatomic, assign) CGFloat scaleBtnY;
;
@property(nonatomic, assign) CGFloat scaleStartBtnX;

@property(nonatomic, assign) CGFloat scaleNextBtnX;


@property(nonatomic, assign) NSInteger currentTimeMinutes;
@property(nonatomic, assign) NSInteger currentTimeSeconds;

@property(nonatomic, assign) NSInteger totalTime;

@property(nonatomic, strong) UILabel *label;


@end

@implementation HearderView


- (void)dealloc
{
    [_timer invalidate];
    [_imageTurnTimer invalidate];
    [_lrcTimer invalidate];
}

+ (HearderView *)shareInstance
{
    static HearderView *hearderView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        hearderView = [[HearderView alloc] init];
    });
    return hearderView;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    y = 0;
    _totalDistance = 0;
    UITouch *touch = [touches anyObject];
    self.startPoint = [touch locationInView:self];
    _startY = self.frame.origin.y;
    [self.HeaderViewDelegate sendBegin];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint newPoint = [touch locationInView:self];
    CGPoint lastPoint = [touch previousLocationInView:self];
    y = newPoint.y - lastPoint.y;
    [self.HeaderViewDelegate sendHeaderViewSize:y];
    if ((int)self.frame.origin.y >= (int)(74 * (_HEIGHT / _DEFAULT_HEIGHT)) && (int)(_HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT)) > (int)self.frame.origin.y) {
        _totalDistance += y;
    }
    if (y != 0 && _totalDistance > 5 && self.frame.origin.y < _HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT) && (int)_startY != (int)(_HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT)) && (int)_totalDistance <= (int)((_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)))) {
        CGFloat scaleDistance = 5 - _totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * 5;
        
        _titleImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 + scaleDistance, 1 + scaleDistance);
        
        CGFloat scaleX = self.frame.size.width / 2 - 37 * (_WIDTH / _DEFAULT_WIDTH)  - _totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * (self.frame.size.width / 2 - 37 * (_WIDTH / _DEFAULT_WIDTH));
        
        CGFloat scaleY = self.frame.size.height / 3 - 32 * (_HEIGHT / _DEFAULT_HEIGHT) - _totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * (self.frame.size.height / 3 - 32 * (_HEIGHT / _DEFAULT_HEIGHT));
        _titleImageView.center = CGPointMake(37 * (_WIDTH / _DEFAULT_WIDTH) + scaleX, 32 * (_HEIGHT / _DEFAULT_HEIGHT) + scaleY);
        
        _progressSlider.alpha = 1 - _totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * 2;
        _likeBtn.alpha = 1 - _totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * 2;
        _downloadBtn.alpha = 1 - _totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * 2;
        _orderButton.alpha = 1 - _totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * 2;
        _listButton.alpha = 1 - _totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * 2;
        _lrcTableView.alpha = 1 - _totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * 4;
        
        _title.alpha = 1 - _totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT));
        _singerName.alpha = 1 - _totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT));
        
        _scaleBtnX = _totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * (_WIDTH / 2 - 115 * (_WIDTH / _DEFAULT_WIDTH));
        
        _scaleBtnY = _totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT));
        
        _scaleStartBtnX = _totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * (_WIDTH / 2 - 145 * (_WIDTH / _DEFAULT_WIDTH));
        
        _scaleNextBtnX = _totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * (_WIDTH / 2 - 175 * (_WIDTH / _DEFAULT_WIDTH));
        
        _lastButton.center = CGPointMake(_WIDTH / 2 - 130 * (_WIDTH / _DEFAULT_WIDTH) + 20 * (_WIDTH / _DEFAULT_WIDTH) + _scaleBtnX, _HEIGHT - 124 * (_HEIGHT / _DEFAULT_HEIGHT) - _scaleBtnY);
        
        _startButton.center = CGPointMake(_WIDTH / 2 + _scaleStartBtnX, _HEIGHT - 124 * (_HEIGHT / _DEFAULT_HEIGHT) - _scaleBtnY);
        _nextButton.center = CGPointMake(_WIDTH / 2 + 90 * (_WIDTH / _DEFAULT_WIDTH) + 20 * (_WIDTH / _DEFAULT_WIDTH) + _scaleNextBtnX, _HEIGHT - 124 * (_HEIGHT / _DEFAULT_HEIGHT) - _scaleBtnY);
        
        _lastButton.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 + scaleDistance / 15, 1 + scaleDistance / 15);
        _startButton.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 + scaleDistance / 5, 1 + scaleDistance / 5);
        _nextButton.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 + scaleDistance / 15, 1 + scaleDistance / 15);
        
        
    } else if (y != 0 && _totalDistance < -5 && (int)self.frame.origin.y > (int)(74 * (_HEIGHT / _DEFAULT_HEIGHT)) && (int)_startY != (int)(74 * (_HEIGHT / _DEFAULT_HEIGHT)) && (int)_totalDistance >= (int)(-(_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)))) {
        
        CGFloat scaleDistance = -_totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * 5;
        
        _titleImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 + scaleDistance, 1 + scaleDistance);
        CGFloat scaleX = -_totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * (self.frame.size.width / 2 - 37 * (_WIDTH / _DEFAULT_WIDTH));
        CGFloat scaleY = (-_totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT))) * (self.frame.size.height / 3 - 32 * (_HEIGHT / _DEFAULT_HEIGHT));
        _titleImageView.center = CGPointMake(37 * (_WIDTH / _DEFAULT_WIDTH) + scaleX, 32 * (_HEIGHT / _DEFAULT_HEIGHT) + scaleY);
        
        _progressSlider.alpha = -_totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT));
        _likeBtn.alpha = -_totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT));
        _downloadBtn.alpha = -_totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT));
        
        _title.alpha = -_totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT));
        
        _orderButton.alpha = -_totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT));
        _listButton.alpha = -_totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT));
        _lrcTableView.alpha = -_totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT));
        _singerName.alpha = -_totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT));
        
        _scaleBtnX = -_totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * (_WIDTH / 2 - 115 * (_WIDTH / _DEFAULT_WIDTH));
        
        _scaleBtnY = -_totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT));
        
        
        _scaleStartBtnX = -_totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * (_WIDTH / 2 - 145 * (_WIDTH / _DEFAULT_WIDTH));
        
        _scaleNextBtnX = -_totalDistance / (_HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT)) * (_WIDTH / 2 - 175 * (_WIDTH / _DEFAULT_WIDTH));
        
        _lastButton.center = CGPointMake(_WIDTH - 240 * (_WIDTH / _DEFAULT_WIDTH) + 15 * (_WIDTH / _DEFAULT_WIDTH) - _scaleBtnX, 35 * (_HEIGHT / _DEFAULT_HEIGHT) + _scaleBtnY);
        
        _startButton.center = CGPointMake(_WIDTH - 160 * (_WIDTH / _DEFAULT_WIDTH) + 15 * (_WIDTH / _DEFAULT_WIDTH) - _scaleStartBtnX, 35 * (_HEIGHT / _DEFAULT_HEIGHT) + _scaleBtnY);
        
        _nextButton.center = CGPointMake(_WIDTH - 80 * (_WIDTH / _DEFAULT_WIDTH) + 15 * (_WIDTH / _DEFAULT_WIDTH) - _scaleNextBtnX, 35 * (_HEIGHT / _DEFAULT_HEIGHT) + _scaleBtnY);
        
        _lastButton.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 + scaleDistance / 5.0 / 3.0, 1 + scaleDistance / 5.0 / 3.0);
        _startButton.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 + scaleDistance / 5.0, 1 + scaleDistance / 5.0);
        _nextButton.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 + scaleDistance / 5.0 / 3.0, 1 + scaleDistance / 5 / 3.0);
        
    }
    
    
    
}



- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (y > 0 && (int)self.frame.origin.y <= (int)(_HEIGHT - 64 * (_HEIGHT / _DEFAULT_HEIGHT))) {
        [UIView animateWithDuration:0.8 animations:^{
            _titleImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
            _titleImageView.center = CGPointMake(37 * (_WIDTH / _DEFAULT_WIDTH), 32 * (_HEIGHT / _DEFAULT_HEIGHT));
            
            _lastButton.frame = CGRectMake(_WIDTH - 240 * (_WIDTH / _DEFAULT_WIDTH), 15, 30 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT));
            _startButton.frame = CGRectMake(_WIDTH - 160 * (_WIDTH / _DEFAULT_WIDTH), 15, 30 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT));
            _nextButton.frame = CGRectMake(_WIDTH - 80 * (_WIDTH / _DEFAULT_WIDTH), 15, 30 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT));
            
            _progressSlider.alpha = 0;
            _likeBtn.alpha = 0;
            _downloadBtn.alpha = 0;
            _title.alpha = 0;
            _lrcTableView.alpha = 0;
            _listButton.alpha = 0;
            _orderButton.alpha = 0;
            _singerName.alpha = 0;
            
        }];
       
    } else if (y <= 0 && (int)self.frame.origin.y > (int)(74 * (_HEIGHT / _DEFAULT_HEIGHT))) {
        [UIView animateWithDuration:0.8 animations:^{
            _titleImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 6, 6);
            _titleImageView.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 3);
//            NSLog(@"%f", self.frame.size.height);
            _lastButton.frame = CGRectMake(_WIDTH / 2 - 130 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 144 * (_HEIGHT / _DEFAULT_HEIGHT), 40 * (_WIDTH / _DEFAULT_WIDTH), 40 * (_HEIGHT / _DEFAULT_HEIGHT));
            
            _startButton.frame = CGRectMake(_WIDTH / 2 - 30 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 154 * (_HEIGHT / _DEFAULT_HEIGHT), 60 * (_WIDTH / _DEFAULT_WIDTH), 60 * (_HEIGHT / _DEFAULT_HEIGHT));
            
            _nextButton.frame = CGRectMake(_WIDTH / 2 + 90 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 144 * (_HEIGHT / _DEFAULT_HEIGHT), 40 * (_WIDTH / _DEFAULT_WIDTH), 40 * (_HEIGHT / _DEFAULT_HEIGHT));
            _progressSlider.alpha = 1;
            _likeBtn.alpha = 1;
            _downloadBtn.alpha = 1;
            _title.alpha = 1;
            _lrcTableView.alpha = 1;
            _listButton.alpha = 1;
            _orderButton.alpha = 1;
            _singerName.alpha = 1;
        }];
        
    }
    y = 0;
    _totalDistance = 0;
    [self.HeaderViewDelegate sendOver];
}

- (void)createView
{
    _label = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, self.frame.size.width - 200, 30)];
    _backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _WIDTH, _HEIGHT - 74 * (_HEIGHT / _DEFAULT_HEIGHT))];
    [self addSubview:_backgroundImageView];
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    _backgroundView = [[UIVisualEffectView alloc] initWithEffect:effect];
    _backgroundView.frame = CGRectMake(0, 0, _WIDTH, _HEIGHT - 74 * (_HEIGHT / _DEFAULT_HEIGHT));
    [self addSubview:_backgroundView];
    [_backgroundImageView setImage:[UIImage imageNamed:@"bgImage"]];
    
    self.backgroundColor = [UIColor colorWithRed:210/255.0 green:233/255.0 blue:240/255.0 alpha:1];
    
    _LRCDictionary = [NSMutableDictionary dictionary];
    _timeArray = [NSMutableArray array];
    
    _title = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, _WIDTH - 20, 40 * (_HEIGHT / _DEFAULT_HEIGHT))];
    [self addSubview:_title];
    _title.textAlignment = NSTextAlignmentCenter;
    _title.alpha = 0;
    
    _title.textColor = [UIColor colorWithRed:30/255.0 green:28/255.0 blue:42/255.0 alpha:1];
    _title.font = [UIFont systemFontOfSize:26];
    _title.lineBreakMode = UILineBreakModeMiddleTruncation;
    
    _singerName = [[UILabel alloc] initWithFrame:CGRectMake(10, 60 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 20, 20 * (_HEIGHT / _DEFAULT_HEIGHT))];
    [self addSubview:_singerName];
    _singerName.textAlignment = NSTextAlignmentCenter;
    _singerName.textColor = [UIColor colorWithRed:30/255.0 green:28/255.0 blue:42/255.0 alpha:1];
    _singerName.alpha = 0;
    
    _titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15 * (_WIDTH / _DEFAULT_WIDTH), 10, 44 * (_HEIGHT / _DEFAULT_HEIGHT), 44 * (_HEIGHT / _DEFAULT_HEIGHT))];
    _titleImageView.backgroundColor = [UIColor whiteColor];
    _titleImageView.layer.cornerRadius = 22 * (_HEIGHT / _DEFAULT_HEIGHT);
    _titleImageView.layer.masksToBounds = YES;
    [_titleImageView setImage:[UIImage imageNamed:@"placeholder"]];
    _titleImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:_titleImageView];
    _centreView = [UIButton buttonWithType:UIButtonTypeCustom];
    _centreView.frame = CGRectMake(0, 0, _titleImageView.frame.size.width / 4, _titleImageView.frame.size.height / 4);
    [_titleImageView addSubview:_centreView];
    _centreView.center = CGPointMake(_titleImageView.frame.size.width / 2, _titleImageView.frame.size.height / 2);
    _centreView.backgroundColor = [UIColor whiteColor];
    _centreView.layer.cornerRadius = _titleImageView.frame.size.height / 8;
    _centreView.layer.masksToBounds = YES;
    _centreView.alpha = 0.5;
    [_centreView addTarget:self action:@selector(startAndStop) forControlEvents:UIControlEventTouchUpInside];
    _titleImageView.alpha = 1;
    
    _listTableView = [[UITableView alloc] initWithFrame:CGRectMake(65 * (_WIDTH / _DEFAULT_WIDTH), 100 * (_HEIGHT / _DEFAULT_HEIGHT), 245 * (_WIDTH / _DEFAULT_WIDTH), 245 * (_HEIGHT / _DEFAULT_HEIGHT)) style:UITableViewStylePlain];
    _listTableView.backgroundColor = [UIColor clearColor];
    [_backgroundView addSubview:_listTableView];
    _listTableView.delegate = self;
    _listTableView.dataSource = self;
    _listTableView.tag = 1;
    _listTableView.alpha = 0;
    _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _listTableView.showsHorizontalScrollIndicator = NO;
    _listTableView.showsVerticalScrollIndicator = NO;
    
    _lastButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _lastButton.frame = CGRectMake(_WIDTH - 240  * (_WIDTH / _DEFAULT_WIDTH), 15, 30 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT));
    [_lastButton addTarget:self action:@selector(lastMusic) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_lastButton];
    [_lastButton setImage:[UIImage imageNamed:@"aboveMusic"] forState:UIControlStateNormal];
    
    _startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _startButton.frame = CGRectMake(_WIDTH - 160  * (_WIDTH / _DEFAULT_WIDTH), 15, 30 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT));
    [_startButton addTarget:self action:@selector(startMusic) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_startButton];
    [_startButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
    
    _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _nextButton.frame = CGRectMake(_WIDTH - 80 * (_WIDTH / _DEFAULT_WIDTH), 15, 30 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT));
    [_nextButton addTarget:self action:@selector(nextMusic) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_nextButton];
    [_nextButton setImage:[UIImage imageNamed:@"nextMusic"] forState:UIControlStateNormal];
    
    
    _downloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _downloadBtn.frame = CGRectMake(_WIDTH - 50 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 370 * (_HEIGHT / _DEFAULT_HEIGHT), 35 * (_WIDTH / _DEFAULT_WIDTH), 35 * (_HEIGHT / _DEFAULT_HEIGHT));
//    [self addSubview:_downloadBtn];
    
    _listButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    _listButton.frame = CGRectMake(_WIDTH - 50 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 270 * (_HEIGHT / _DEFAULT_HEIGHT), 35 * (_WIDTH / _DEFAULT_WIDTH), 35 * (_HEIGHT / _DEFAULT_HEIGHT));
//    [self addSubview:_listButton];
    
    _listButton.frame = CGRectMake(_WIDTH - 100 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 290 * (_HEIGHT / _DEFAULT_HEIGHT), 40 * (_WIDTH / _DEFAULT_WIDTH), 40 * (_HEIGHT / _DEFAULT_HEIGHT));
    [self addSubview:_listButton];
    
    _orderButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    _orderButton.frame = CGRectMake(_WIDTH - 50 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 320 * (_HEIGHT / _DEFAULT_HEIGHT), 35 * (_WIDTH / _DEFAULT_WIDTH), 35 * (_HEIGHT / _DEFAULT_HEIGHT));
    
    _orderButton.frame = CGRectMake(_WIDTH / 2 - 20 * (_HEIGHT / _DEFAULT_HEIGHT), _HEIGHT - 290 * (_HEIGHT / _DEFAULT_HEIGHT), 40 * (_WIDTH / _DEFAULT_WIDTH), 40 * (_HEIGHT / _DEFAULT_HEIGHT));
    [self addSubview:_orderButton];
    
    
    _likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    _likeBtn.frame = CGRectMake(_WIDTH - 53 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 420 * (_HEIGHT / _DEFAULT_HEIGHT), 40 * (_WIDTH / _DEFAULT_WIDTH), 40 * (_HEIGHT / _DEFAULT_HEIGHT));
    
    _likeBtn.frame = CGRectMake(_WIDTH / 2 - 130 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 290 * (_HEIGHT / _DEFAULT_HEIGHT), 40 * (_WIDTH / _DEFAULT_WIDTH), 40 * (_HEIGHT / _DEFAULT_HEIGHT));
    [self addSubview:_likeBtn];
    
    [_listButton addTarget:self action:@selector(moveToList) forControlEvents:UIControlEventTouchUpInside];
    [_downloadBtn addTarget:self action:@selector(download:) forControlEvents:UIControlEventTouchUpInside];
    [_likeBtn addTarget:self action:@selector(collect:) forControlEvents:UIControlEventTouchUpInside];
    [_orderButton addTarget:self action:@selector(changeOrder:) forControlEvents:UIControlEventTouchUpInside];
    [_listButton setImage:[UIImage imageNamed:@"iconfont-bofangliebiao"] forState:UIControlStateNormal];
    [_downloadBtn setImage:[UIImage imageNamed:@"iconfont-xiazai2"] forState:UIControlStateNormal];
    [_likeBtn setImage:[UIImage imageNamed:@"iconfont-xihuan3"] forState:UIControlStateNormal];
    [_orderButton setImage:[UIImage imageNamed:@"iconfont-shunxuxunhuan"] forState:UIControlStateNormal];
    
    
    
    _totalTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_WIDTH - 75 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 184 * (_HEIGHT / _DEFAULT_HEIGHT), 60 * (_WIDTH / _DEFAULT_WIDTH), 20 * (_HEIGHT / _DEFAULT_HEIGHT))];
    [self addSubview:_totalTimeLabel];
    _currentTime = [[UILabel alloc] initWithFrame:CGRectMake(30 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 184 * (_HEIGHT / _DEFAULT_HEIGHT), 90 * (_WIDTH / _DEFAULT_WIDTH), 20 * (_HEIGHT / _DEFAULT_HEIGHT))];
    [self addSubview:_currentTime];
    
#pragma -mark lrcTableView
    _lrcTableView = [[UITableView alloc] initWithFrame:CGRectMake(30 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 300 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 60 * (_WIDTH / _DEFAULT_WIDTH), 80 * (_HEIGHT / _DEFAULT_HEIGHT)) style:UITableViewStylePlain];
//    [self addSubview:_lrcTableView];
    _lrcTableView.backgroundColor = [UIColor clearColor];
    _lrcTableView.delegate = self;
    _lrcTableView.dataSource = self;
    _lrcTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _lrcTableView.showsVerticalScrollIndicator = NO;
    _lrcTableView.tag = 0;
    _lrcTableView.backgroundColor = [UIColor colorWithRed:210/255.0 green:233/255.0 blue:240/255.0 alpha:1];
    _lrcTableView.backgroundColor = [UIColor clearColor];
    
//    _currentTime.textColor = [UIColor grayColor];
//    _totalTimeLabel.textColor = [UIColor grayColor];
    
    _progressSlider = [[UISlider alloc] initWithFrame:CGRectMake(30 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 216 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 60 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT))];
    _progressSlider.maximumTrackTintColor = [UIColor clearColor];
    _progressSlider.minimumTrackTintColor = [UIColor blueColor];
    _progressSlider.backgroundColor = [UIColor clearColor];
    _progressBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
    _progressBar.frame = CGRectMake(36 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 202.7 * (_HEIGHT / _DEFAULT_HEIGHT), _WIDTH - 72 * (_WIDTH / _DEFAULT_WIDTH), 30 * (_HEIGHT / _DEFAULT_HEIGHT));
    [self addSubview:_progressBar];
    _progressBar.trackTintColor = [UIColor colorWithRed:194 / 255.0 green:194 / 255.0 blue:194 / 255.0 alpha:1];
    _progressBar.progressTintColor = [UIColor grayColor];
    _totalTimeLabel.text = @"00:00";
    _currentTime.text = @"00:00";
    _progressBar.progress = 0;
    
    _progressSlider.alpha = 0;
    _likeBtn.alpha = 0;
    _downloadBtn.alpha = 0;
    [self addSubview:_progressSlider];
    [_progressSlider setThumbImage:[UIImage imageNamed:@"soundSlider"] forState:UIControlStateNormal];
    [_progressSlider setThumbImage:[UIImage imageNamed:@"soundSlider"] forState:UIControlStateHighlighted];
    _progressSlider.value = 0.0;
    [_progressSlider addTarget:self action:@selector(moveSlider) forControlEvents:UIControlEventValueChanged];
    
    [self bringSubviewToFront:_lastButton];
    [self bringSubviewToFront:_startButton];
    [self bringSubviewToFront:_nextButton];
    [self bringSubviewToFront:_listButton];
    [self bringSubviewToFront:_orderButton];
}

- (void)changeOrder:(UIButton *)button
{
    if ([MusicPlayer1 shareInstance].orderManager == 0) {
        [MusicPlayer1 shareInstance].orderManager = 1;
        [button setImage:[UIImage imageNamed:@"iconfont-ttpodicon"] forState:UIControlStateNormal];
    } else if ([MusicPlayer1 shareInstance].orderManager == 1) {
        [MusicPlayer1 shareInstance].orderManager = 2;
        [button setImage:[UIImage imageNamed:@"iconfont-suijibofang"] forState:UIControlStateNormal];
    } else if ([MusicPlayer1 shareInstance].orderManager == 2) {
        [MusicPlayer1 shareInstance].orderManager = 0;
        [button setImage:[UIImage imageNamed:@"iconfont-shunxuxunhuan"] forState:UIControlStateNormal];
    }
}

- (void)moveToList
{
    if (_isList) {
        [UIView animateWithDuration:0.8 animations:^{
            _titleImageView.alpha = 1.0f;
            _listTableView.alpha = 0.0f;
            _isList = NO;
        }];
    } else {
        [UIView animateWithDuration:0.8 animations:^{
            _titleImageView.alpha = 0.0f;
            _listTableView.alpha = 0.8f;
            _isList = YES;
        }];
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 0) {
        static NSString *identifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;//该表格选中后没有颜色
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        if (indexPath.row == _lrcLineNumber) {
            cell.textLabel.text = _LRCDictionary[_timeArray[indexPath.row]];
            cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];
            cell.textLabel.font = [UIFont systemFontOfSize:15];
        } else {
            cell.textLabel.text = _LRCDictionary[_timeArray[indexPath.row]];
            cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
            cell.textLabel.font = [UIFont systemFontOfSize:13];
        }
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        
        return cell;
    } else {
        static NSString *identifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        }
        MusicModel *model = [[MusicPlayer1 shareInstance].list objectAtIndex:indexPath.row];
        cell.textLabel.text = model.songName;
        cell.detailTextLabel.text = model.singerName;
        if (indexPath.row % 2 == 0) {
            cell.backgroundColor = [UIColor colorWithRed:227/255.0 green:233/255.0 blue:240/255.0 alpha:1];
        } else {
            cell.backgroundColor = [UIColor clearColor];
            cell.textLabel.backgroundColor = [UIColor clearColor];
            cell.detailTextLabel.backgroundColor = [UIColor clearColor];
        }
        return cell;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 0) {
        return _timeArray.count;
    } else {
        return [MusicPlayer1 shareInstance].list.count;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 1) {
        [MusicPlayer1 shareInstance].currentMusic = indexPath.row;
        if ([MusicPlayer1 shareInstance].currentMusic >= 0 && [MusicPlayer1 shareInstance].currentMusic < [MusicPlayer1 shareInstance].list.count) {
            [_timer invalidate];
            [_lrcTimer invalidate];
            [_imageTurnTimer invalidate];
            [_LRCDictionary removeAllObjects];
            [_timeArray removeAllObjects];
            _currentTime.text = @"00:00";
            MusicModel *model = [[MusicPlayer1 shareInstance].list objectAtIndex:[MusicPlayer1 shareInstance].currentMusic];
            if ([MusicPlayer1 shareInstance].isDownLoad) {
                NSString *homePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
                NSString *url = [homePath stringByAppendingPathComponent:model.url];
                [[MusicPlayer1 shareInstance].moviePlayer setContentURL:[NSURL fileURLWithPath:url]];
            } else {
                [[MusicPlayer1 shareInstance].moviePlayer setContentURL:[NSURL URLWithString:model.url]];
            }

            [[MusicPlayer1 shareInstance] play];
            [HearderView shareInstance].progressSlider.value = 0;
            [MusicPlayer1 shareInstance].lrc = nil;
            [_lrcTableView reloadData];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 0) {
        return 35;
    }
    return 40;
}

#pragma mark 得到歌词
- (void)initLRC {
//    _LRCDictionary = [NSMutableDictionary dictionary];
//    _timeArray = [NSMutableArray array];
    NSString *contentStr = [NSString stringWithString:[MusicPlayer1 shareInstance].lrc];
    NSArray *array = [contentStr componentsSeparatedByString:@"\n"];
    for (int i = 0; i < [array count]; i++) {
        NSString *linStr = [array objectAtIndex:i];
        NSArray *lineArray = [linStr componentsSeparatedByString:@"]"];
        if ([lineArray[0] length] > 8) {
            NSString *str1 = [linStr substringWithRange:NSMakeRange(3, 1)];
            NSString *str2 = [linStr substringWithRange:NSMakeRange(6, 1)];
            if ([str1 isEqualToString:@":"] && [str2 isEqualToString:@"."]) {
                NSString *lrcStr = [lineArray objectAtIndex:1];
                NSString *timeStr = [[lineArray objectAtIndex:0] substringWithRange:NSMakeRange(1, 5)];//分割区间求歌词时间
                //把时间 和 歌词 加入词典
                [_LRCDictionary setObject:lrcStr forKey:timeStr];
                [_timeArray addObject:timeStr];//timeArray的count就是行数
            }
        }
    }
}
#pragma mark 动态显示歌词
- (void)displaySondWord:(NSUInteger)time {
    //    NSLog(@"time = %u",time);
    for (int i = 0; i < [_timeArray count]; i++) {
        
        NSArray *array = [_timeArray[i] componentsSeparatedByString:@":"];//把时间转换成秒
        NSUInteger currentTime = [array[0] intValue] * 60 + [array[1] intValue];
        if (i == [_timeArray count]-1) {
            //求最后一句歌词的时间点
            NSArray *array1 = [_timeArray[_timeArray.count-1] componentsSeparatedByString:@":"];
            NSUInteger currentTime1 = [array1[0] intValue] * 60 + [array1[1] intValue];
            if (time > currentTime1) {
                [self updateLrcTableView:i];
                break;
            }
        } else {
            //求出第一句的时间点，在第一句显示前的时间内一直加载第一句
            NSArray *array2 = [_timeArray[0] componentsSeparatedByString:@":"];
            NSUInteger currentTime2 = [array2[0] intValue] * 60 + [array2[1] intValue];
            if (time < currentTime2) {
                [self updateLrcTableView:0];
                //                NSLog(@"马上到第一句");
                break;
            }
            //求出下一步的歌词时间点，然后计算区间
            NSArray *array3 = [_timeArray[i+1] componentsSeparatedByString:@":"];
            NSUInteger currentTime3 = [array3[0] intValue] * 60 + [array3[1] intValue];
            if (time >= currentTime && time <= currentTime3) {
                [self updateLrcTableView:i];
                break;
            }
            
        }
    }
}

#pragma mark 动态更新歌词表歌词
- (void)updateLrcTableView:(NSUInteger)lineNumber {
    //重新载入 歌词列表lrcTabView
    _lrcLineNumber = lineNumber;
    [_lrcTableView reloadData];
    //使被选中的行移到中间
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lineNumber inSection:0];
    [_lrcTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}


- (void)startAndStop
{
    [self startMusic];
}

- (void)startLoadLrc
{
    _lrcTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeLrc) userInfo:nil repeats:YES];
}

- (void)changeLrc
{
    [self displaySondWord:[MusicPlayer1 shareInstance].moviePlayer.currentPlaybackTime];
}

- (void)startTurn
{
    _currentTimeMinutes = [MusicPlayer1 shareInstance].moviePlayer.duration / 60;
    _currentTimeSeconds = (NSInteger)[MusicPlayer1 shareInstance].moviePlayer.duration % 60;
    
    _totalTime = _currentTimeSeconds + _currentTimeMinutes * 60;
    _progressSlider.maximumValue = [MusicPlayer1 shareInstance].moviePlayer.duration;
    _progressSlider.minimumValue = 0.0;
    if (_currentTimeSeconds < 10) {
        _totalTimeLabel.text = [NSString stringWithFormat:@"%ld:0%ld", _currentTimeMinutes, _currentTimeSeconds];
    } else {
        _totalTimeLabel.text = [NSString stringWithFormat:@"%ld:%ld", _currentTimeMinutes, _currentTimeSeconds];
    }
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(turnImage) userInfo:nil repeats:YES];
    _imageTurnTimer = [NSTimer scheduledTimerWithTimeInterval:0.07 target:self selector:@selector(turn) userInfo:nil repeats:YES];

}

- (void)turn
{
    _titleImageView.transform = CGAffineTransformRotate(_titleImageView.transform, M_PI / 360);
}

- (void)turnImage
{
    [_progressBar setProgress:[MusicPlayer1 shareInstance].moviePlayer.playableDuration / [MusicPlayer1 shareInstance].moviePlayer.duration animated:YES];
    _progressSlider.value = [MusicPlayer1 shareInstance].moviePlayer.currentPlaybackTime;
    if ((int)[MusicPlayer1 shareInstance].moviePlayer.currentPlaybackTime % 60 < 10) {
        _currentTime.text = [NSString stringWithFormat:@"%d:0%d",(int)[MusicPlayer1 shareInstance].moviePlayer.currentPlaybackTime / 60, (int)[MusicPlayer1 shareInstance].moviePlayer.currentPlaybackTime % 60];
    } else {
        _currentTime.text = [NSString stringWithFormat:@"%d:%d",(int)[MusicPlayer1 shareInstance].moviePlayer.currentPlaybackTime / 60, (int)[MusicPlayer1 shareInstance].moviePlayer.currentPlaybackTime % 60];
    }
     if (NSClassFromString(@"MPNowPlayingInfoCenter")) {
    [[MusicPlayer1 shareInstance].dict setObject:[NSNumber numberWithDouble:(double)[MusicPlayer1 shareInstance].moviePlayer.currentPlaybackTime] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:[MusicPlayer1 shareInstance].dict];
     }

}
- (void)moveSlider
{
    [MusicPlayer1 shareInstance].moviePlayer.currentPlaybackTime = _progressSlider.value;
}

- (void)lastMusic
{
    if ([MusicPlayer1 shareInstance].list.count == 0) {
        [[MusicPlayer1 shareInstance].moviePlayer pause];
        [[MusicPlayer1 shareInstance].moviePlayer setContentURL:nil];
        [MusicPlayer1 shareInstance].isPlay = NO;
        [MusicPlayer1 shareInstance].titleImage.image = [UIImage imageNamed:@"placeholder"];
        [_backgroundImageView setImageWithURL:nil placeholderImage:[UIImage imageNamed:@"bgImage"]];
        [_titleImageView setImage:[UIImage imageNamed:@"placeholder"]];
        [_startButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
        _centreView.alpha = 0.8;
        _progressSlider.value = 0;
        _title.text = nil;
        _singerName.text = nil;
        [_timer invalidate];
        [_imageTurnTimer invalidate];
        [_lrcTimer invalidate];
        _currentTime.text = @"00:00";
        [_LRCDictionary removeAllObjects];
        [_timeArray removeAllObjects];
        [_lrcTableView reloadData];
        [_listTableView reloadData];
        [_progressBar setProgress:0.0 animated:NO];
        return;
    }
    if ([MusicPlayer1 shareInstance].currentMusic > 0) {
//        [_timer invalidate];
//        [_lrcTimer invalidate];
//        [_LRCDictionary removeAllObjects];
//        [_timeArray removeAllObjects];
//        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[MusicPlayer1 shareInstance].currentMusic - 1 inSection:0];
//        [_listTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
//        _currentTime.text = @"00:00";
        MusicModel *model;
        if ([MusicPlayer1 shareInstance].orderManager != 2) {
//            model = [[MusicPlayer1 shareInstance].list objectAtIndex:
                --[MusicPlayer1 shareInstance].currentMusic;
        } else {
            [MusicPlayer1 shareInstance].currentMusic = random() % ([MusicPlayer1 shareInstance].list.count - 1);
//            model = [[MusicPlayer1 shareInstance].list objectAtIndex:[MusicPlayer1 shareInstance].currentMusic];
        }
        model = [[MusicPlayer1 shareInstance].list objectAtIndex:[MusicPlayer1 shareInstance].currentMusic];
        if ([MusicPlayer1 shareInstance].isDownLoad) {
            NSString *homePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
            NSString *url = [homePath stringByAppendingPathComponent:model.url];
            [[MusicPlayer1 shareInstance].moviePlayer setContentURL:[NSURL fileURLWithPath:url]];
        } else {
           [[MusicPlayer1 shareInstance].moviePlayer setContentURL:[NSURL URLWithString:model.url]];
        }
        [[MusicPlayer1 shareInstance] play];
//        [HearderView shareInstance].progressSlider.value = 0;
//        [MusicPlayer1 shareInstance].lrc = nil;
//        [_lrcTableView reloadData];
    } else if ([MusicPlayer1 shareInstance].currentMusic == 0) {
        [MusicPlayer1 shareInstance].currentMusic = [MusicPlayer1 shareInstance].list.count - 1;
//        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[MusicPlayer1 shareInstance].currentMusic inSection:0];
        //        [_listTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        //        _currentTime.text = @"00:00";
        MusicModel *model;
//        if ([MusicPlayer1 shareInstance].orderManager != 2) {
//            model = [[MusicPlayer1 shareInstance].list objectAtIndex:[MusicPlayer1 shareInstance].currentMusic];
//        } else {
//            [MusicPlayer1 shareInstance].currentMusic = random() % ([MusicPlayer1 shareInstance].list.count - 1);
//            model = [[MusicPlayer1 shareInstance].list objectAtIndex:[MusicPlayer1 shareInstance].currentMusic];
//        }
        model = [[MusicPlayer1 shareInstance].list objectAtIndex:[MusicPlayer1 shareInstance].currentMusic];
        if ([MusicPlayer1 shareInstance].isDownLoad) {
            NSString *homePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
            NSString *url = [homePath stringByAppendingPathComponent:model.url];
            [[MusicPlayer1 shareInstance].moviePlayer setContentURL:[NSURL fileURLWithPath:url]];
        } else {
            [[MusicPlayer1 shareInstance].moviePlayer setContentURL:[NSURL URLWithString:model.url]];
        }
        [[MusicPlayer1 shareInstance] play];
        //        [HearderView shareInstance].progressSlider.value = 0;
        //        [MusicPlayer1 shareInstance].lrc = nil;
//        [_lrcTableView reloadData];
    }
    _centreView.alpha = 0.4;
    [_startButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
}

- (void)startMusic
{
    if ([MusicPlayer1 shareInstance].list.count > 0 && [MusicPlayer1 shareInstance].firstPlay == NO) {
        [HearderView shareInstance].currentList = 0;
        [MusicPlayer1 shareInstance].firstPlay = YES;
        MusicModel *model = [[MusicPlayer1 shareInstance].collectList firstObject];
        [[MusicPlayer1 shareInstance].moviePlayer setContentURL:[NSURL URLWithString:model.url]];
        [[MusicPlayer1 shareInstance] play];
        [MusicPlayer1 shareInstance].isPlay = YES;
        [_startButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
    } else {
        if ([[MusicPlayer1 shareInstance] isPlay]) {
            [[MusicPlayer1 shareInstance].moviePlayer pause];
            [MusicPlayer1 shareInstance].isPlay = NO;
            [_timer setFireDate:[NSDate distantFuture]];
            [_imageTurnTimer setFireDate:[NSDate distantFuture]];
            [_startButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
            _centreView.alpha = 0.8;
        } else {
            [[MusicPlayer1 shareInstance].moviePlayer play];
            [MusicPlayer1 shareInstance].isPlay = YES;
            [_timer setFireDate:[NSDate date]];
            [_imageTurnTimer setFireDate:[NSDate date]];
            _centreView.alpha = 0.4;
            [_startButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
        }
    }
}

- (void)nextMusic
{
    if ([MusicPlayer1 shareInstance].list.count == 0) {
        [[MusicPlayer1 shareInstance].moviePlayer pause];
        _title.text = nil;
        _singerName.text = nil;
        [MusicPlayer1 shareInstance].titleImage.image = [UIImage imageNamed:@"placeholder"];
        [_backgroundImageView setImageWithURL:nil placeholderImage:[UIImage imageNamed:@"bgImage"]];
        [_titleImageView setImage:[UIImage imageNamed:@"placeholder"]];
        [[MusicPlayer1 shareInstance].moviePlayer setContentURL:nil];
        [MusicPlayer1 shareInstance].isPlay = NO;
         _centreView.alpha = 0.8;
        _progressSlider.value = 0;
        [_timer invalidate];
        [_imageTurnTimer invalidate];
        [_lrcTimer invalidate];
        [_startButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
        _currentTime.text = @"00:00";
        [_LRCDictionary removeAllObjects];
        [_timeArray removeAllObjects];
        [_lrcTableView reloadData];
        [_listTableView reloadData];
        [_progressBar setProgress:0.0 animated:NO];
        return;
    }
    if ([MusicPlayer1 shareInstance].list.count > 0 && [MusicPlayer1 shareInstance].currentMusic < [MusicPlayer1 shareInstance].list.count - 1) {
//        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[MusicPlayer1 shareInstance].currentMusic + 1 inSection:0];
//        [_listTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
//        [_LRCDictionary removeAllObjects];
//        [_timeArray removeAllObjects];
//        _currentTime.text = @"00:00";
//        [_timer invalidate];
//        [_lrcTimer invalidate];
//        [HearderView shareInstance].progressSlider.value = 0;
        MusicModel *model;
        if ([MusicPlayer1 shareInstance].orderManager != 2) {
//            model = [[MusicPlayer1 shareInstance].list objectAtIndex:
                     ++[MusicPlayer1 shareInstance].currentMusic;
        } else {
            [MusicPlayer1 shareInstance].currentMusic = random() % ([MusicPlayer1 shareInstance].list.count - 1);
//            model = [[MusicPlayer1 shareInstance].list objectAtIndex:[MusicPlayer1 shareInstance].currentMusic];
        }
        model = [[MusicPlayer1 shareInstance].list objectAtIndex:[MusicPlayer1 shareInstance].currentMusic];
        if ([MusicPlayer1 shareInstance].isDownLoad) {
            NSString *homePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
            NSString *url = [homePath stringByAppendingPathComponent:model.url];
            [[MusicPlayer1 shareInstance].moviePlayer setContentURL:[NSURL fileURLWithPath:url]];
        } else {
            [[MusicPlayer1 shareInstance].moviePlayer setContentURL:[NSURL URLWithString:model.url]];
        }

        [[MusicPlayer1 shareInstance] play];
//        [MusicPlayer1 shareInstance].lrc = nil;
        [_lrcTableView reloadData];
    } else if ([MusicPlayer1 shareInstance].list.count > 0 && [MusicPlayer1 shareInstance].currentMusic == [MusicPlayer1 shareInstance].list.count - 1) {
        [MusicPlayer1 shareInstance].currentMusic = 0;
        MusicModel *model = [[MusicPlayer1 shareInstance].list objectAtIndex:[MusicPlayer1 shareInstance].currentMusic];
        if ([MusicPlayer1 shareInstance].isDownLoad) {
            NSString *homePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
            NSString *url = [homePath stringByAppendingPathComponent:model.url];
            [[MusicPlayer1 shareInstance].moviePlayer setContentURL:[NSURL fileURLWithPath:url]];
        } else {
            [[MusicPlayer1 shareInstance].moviePlayer setContentURL:[NSURL URLWithString:model.url]];
        }
        
        [[MusicPlayer1 shareInstance] play];
        [_lrcTableView reloadData];
    }
    _centreView.alpha = 0.4;
    [_startButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
}

#pragma -mark 收藏
- (void)collect:(UIButton *)button
{
    if ([MusicPlayer1 shareInstance].list.count > [MusicPlayer1 shareInstance].currentMusic) {
        MusicModel *model = [[MusicPlayer1 shareInstance].list objectAtIndex:[MusicPlayer1 shareInstance].currentMusic];
        NSArray *array = [Database selectData:@""];
        BOOL isOK = YES;
        for (MusicModel *dbModel in array) {
            if ([dbModel.songId integerValue] == [model.songId integerValue]) {
                isOK = NO;
                break;
            }
        }
        if (isOK) {
            
            [_likeBtn setImage:[UIImage imageNamed:@"iconfont-xihuan"] forState:UIControlStateNormal];
            [[MusicPlayer1 shareInstance].collectList addObject:model];
            [Database insertValue:model];
        } else {
            
            [_likeBtn setImage:[UIImage imageNamed:@"iconfont-xihuan3"] forState:UIControlStateNormal];
            [Database deleteValue:model.songId];
            NSArray *array = [Database selectData:@""];
            [[MusicPlayer1 shareInstance].collectList removeAllObjects];
            [[MusicPlayer1 shareInstance].collectList addObjectsFromArray:array];
            if (_currentList == 0) {
                [[MusicPlayer1 shareInstance].list removeAllObjects];
                [[MusicPlayer1 shareInstance].list addObjectsFromArray:array];
                [_listTableView reloadData];
            }
        }
    }
    if (_isSongListView) {
        [self.HeaderViewDelegate reloadSong];
    }
}

#pragma -mark 下载
- (void)download:(UIButton *)button
{
    if ([MusicPlayer1 shareInstance].list.count > [MusicPlayer1 shareInstance].currentMusic) {
        MusicModel *model = [[MusicPlayer1 shareInstance].list  objectAtIndex:[MusicPlayer1 shareInstance].currentMusic];
        NSArray *array = [Database selectDownloadData];
        BOOL isHave = NO;
        for (MusicModel *musicModel in array) {
            if ([model.songId integerValue] == [musicModel.songId integerValue]) {
                isHave = YES;
            }
        }
        if (!isHave) {
            _label.text = @"正在下载";
            _label.center = self.center;
            _label.textAlignment = NSTextAlignmentCenter;
            _label.backgroundColor = [UIColor grayColor];
            _label.textColor = [UIColor whiteColor];
            [self addSubview:_label];
            _label.alpha = 0.9;
            [UIView animateWithDuration:1.5 animations:^{
//                button.frame = CGRectMake(_WIDTH - 50 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 370 * (_HEIGHT / _DEFAULT_HEIGHT), 10 * (_WIDTH / _DEFAULT_WIDTH), 10 * (_HEIGHT / _DEFAULT_HEIGHT));
                _label.alpha = 1;
            } completion:^(BOOL finished) {
//                button.userInteractionEnabled = NO;
                [_label removeFromSuperview];
            }];
            AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            [manager GET:model.url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSString *number = [_USERDEFAULT objectForKey:@"number"];
                NSString *musicName = [NSString stringWithFormat:@"music%@.mp3", number];
                MusicModel *musicModel = [MusicModel getMusicModel];
                musicModel.url = musicName;
                musicModel.singerName = model.singerName;
                musicModel.songName = model.songName;
                musicModel.lrcId = model.songId;
                musicModel.singerId = model.singerId;
                musicModel.songId = model.songId;
                musicModel.musicLength = model.musicLength;
                [Database insertDownloadValue:musicModel];
                [[MusicPlayer1 shareInstance].downloadList addObject:model];
                if (_currentList == 1) {
                    [[MusicPlayer1 shareInstance].list removeAllObjects];
                    [[MusicPlayer1 shareInstance].list addObjectsFromArray:[MusicPlayer1 shareInstance].downloadList];
                    [_listTableView reloadData];
                }
                
                NSString *musicPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:musicName];
                
                [[NSFileManager defaultManager] createFileAtPath:musicPath contents:operation.responseData attributes:nil];
                NSInteger num = [number integerValue];
                number = [NSString stringWithFormat:@"%ld", ++num];
                [_USERDEFAULT setObject:number forKey:@"number"];
                [[HearderView shareInstance].downloadBtn setImage:[UIImage imageNamed:@"iconfont-xiazaiwancheng"] forState:UIControlStateNormal];
                [UIView animateWithDuration:0.8 animations:^{
                    button.frame = CGRectMake(_WIDTH - 50 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 370 * (_HEIGHT / _DEFAULT_HEIGHT), 35 * (_WIDTH / _DEFAULT_WIDTH), 35 * (_HEIGHT / _DEFAULT_HEIGHT));
                } completion:^(BOOL finished) {
                    button.userInteractionEnabled = YES;
                }];
                
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"下载失败" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
                [UIView animateWithDuration:0.8 animations:^{
                    button.frame = CGRectMake(_WIDTH - 50 * (_WIDTH / _DEFAULT_WIDTH), _HEIGHT - 370 * (_HEIGHT / _DEFAULT_HEIGHT), 35 * (_WIDTH / _DEFAULT_WIDTH), 35 * (_HEIGHT / _DEFAULT_HEIGHT));
                } completion:^(BOOL finished) {
                    button.userInteractionEnabled = YES;
                }];
                
            }];
        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"已经下载" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }
}







@end
